define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');

	$.widget('rk.oaTabCtrl', {
		options: {
			tabs: []
		},
		_create: function () {
			var me = this;
			var elem = me.element;
			var opt = me.options;

			elem.data('oaTabCtrl', 'true');

			var tpl = require('page/tmpl/tpl_oa_Tab.tpl');
			var html = rk.templateText(tpl, opt);
			elem.html(html);
			me._initTabs();
			me._trigger('onInit');
		},
		_initTabs: function(){
			var me = this;
			var elem = me.element;
			var opt = me.options;

			var header = me.getHeader();

			var body = me.getBody();
			header.find('>*[idx]').each(function(idx){//自定义属性opt.tabs[i].attrs
				var el = $(this);
				var attrs = opt.tabs[idx].attrs;
				if(attrs){
					for(key in attrs){
						var value = attrs[key];
						el.attr(key, value);
					}
				}
			});
			//初始化按钮
			header.on('click', '>*[idx]', function(){
				me._tabOnClick($(this));
			});
			//初始化子列表
			body.find('[component="oa-tab-child"][idx]').each(function(i){
				var child = $(this);
				var opt_tabs = opt.tabs[i];
				if(opt_tabs.listviewId)child.attr('id', opt_tabs.listviewId);
				if(opt_tabs.controller){
					var childOpt = (opt_tabs.controllerOptions ? opt_tabs.controllerOptions : {});
					child[opt_tabs.controller](childOpt);//初始化为指定的controller
					child[opt_tabs.controller]('setAutoload', opt_tabs.autoload);//缓存ajax信息，分页用
				}
			});
			me._resizeBody();

			$(window).on('resize.oatab', function(){
				window.clearTimeout(me.resizeTimer);
				me.resizeTimer = window.setTimeout(function(){
					me._resizeBody();
				}, 80);
			});
		},
		resizeTimer: null,
		_tabOnClick: function(btn){
			var me = this;
			var elem = me.element;
			var opt = me.options;

			var header = me.getHeader();
			var mybody = me.getBody();

			var idx = btn.attr('idx');
			idx = parseInt(idx);
			//选中效果
			header.find('>*[idx]').removeClass('active');
			btn.addClass('active');
			mybody.find('[component="oa-tab-child"][idx]').hide();
			var activeChild = mybody.find('[component="oa-tab-child"][idx="'+idx+'"]');
			activeChild.show();

			var tabOptions = opt.tabs[idx];
			me._trigger('onTabSelect', null/*event*/, {
	                							index: idx,
	                							tabBtn: btn,
	                							child: activeChild
	                						});
			var child = me._getChild(idx);
			var controller = opt.tabs[idx].controller;
			//加载数据
			if(child[controller])child[controller]('loadData', null, function(){
				//
			});
			btn.attr('loaded', 'true');
		},
		_resizeBody: function(){
			var me = this;
			var elem = me.element;
			var opt = me.options;

			var con = elem.find('.oa-tab:first');
			var header = me.getHeader();
			var mybody = me.getBody();

			var h = con.innerHeight() - header.outerHeight(true);
			if(h < 200)h = 200;

			mybody.height(h);

			if(mybody.isScroll()){
				mybody.updateScroll();
			}else{
				mybody.beScroll();
			}
		},
		getHeader: function(){
			var me = this;
			var elem = me.element;
			return elem.find('.oa-tab-header:first');
		},
		getBody: function(){
			var me = this;
			var elem = me.element;
			return elem.find('.oa-tab-body:first');
		},
		getTab: function(idx){
			var me = this;
			return me.getHeader().find('>*[idx]:eq('+idx+')');
		},
		_getChild: function(idx){
			var me = this;
			return me.getBody().find('[component="oa-tab-child"][idx="'+idx+'"]');
		},
		getSelectedChild: function(){
			var me = this;
			return me.getBody().find('[component="oa-tab-child"][idx]:visible');
	    },
		select: function(idx){
			var me = this;
			me.getTab(idx).click();
		},
		getSelectedTab: function(){
			var me = this;
			var header = me.getHeader();
			return header.find('a.active');
	    },
        _destroy: function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;

            for(var i = 0; i < opt.tabs.length; i++){
            	var idx = i;
            	var controller = opt.tabs[idx].controller;
            	var child = me._getChild(idx);
            	if(child[controller])child[controller]('destroy').off().html('').remove();
            }
            $(window).off('resize.oatab');
        }
	});
});